###########  LAB 3 #############

# create vector with whole number values ranging from 0-12
x <- 0:12

#THE FIRST SET OF THAR DATA
#number of females in each age class
sx<-c(205, 96, 94, 89, 79, 68, 55, 43, 32, 22, 15, 10, 6)
s0<-sx[1]

lx <- sx / s0  #get lx values

gx <- 0:12
for (i in 1:13)
{
	if((i+1) > length(lx) )
	{
		gx[i] = 0
	}
	else
	{
		gx[i] = lx[i+1] / lx[i]
	}
}

#fecundity
mx<- c(0.00, 0.01, 0.14, 0.44, 0.42, 0.47, 0.41, 0.46, 0.49, 0.50, 0.50, 0.41, 0.41)

R0 = sum(lx*mx)

G = sum(lx*mx*x)/sum(lx*mx)

L1 <- GetLMat(mx,gx)

r = log(R0)/G

# Long-term growth rate (LGR) = Dominant Eigen value
LGR = eigen(L1)$values[1]

#Stable age distribution (SAD) = RIGHT dominant EV
SAD = eigen(L1)$vectors[,1]

#Reproductive value (RV) = left dominant EV
RV = eigen(t(L1))$vectors[,1]



########A SECOND SET OF THAR DATA FROM A DIFFERENT POPULATION ##################
x2<-0:12 #vector of ages for second time 

#number of females in each age class for 2nd time
sx2<-c(43, 27, 26, 25, 23, 22, 19, 17, 14, 11, 6, 0, 0)
s02<-sx2[1]

lx2 <- sx2 / s02  #get lx values

gx2 <- 0:12
for (i in 1:13)
{
	if((i+1) > length(lx2) )
	{
		gx2[i] = 0
	}
	else
	{
		gx2[i] = lx2[i+1] / lx2[i]
	}
}

gx2[12] = 0

#fecundity for second time
mx2<-c(0.00, 0.10, 0.40, 0.50, 0.55, 0.60, 0.46, 0.44, 0.50, 0.50, 0.41, 0.00, 0.00)

R02 = sum(lx2*mx2)

G2 = sum(lx2*mx2*x2)/sum(lx2*mx2)

r2 = log(R02)/G2


#Create leslie matrices
L2 = GetLMat(mx2,gx2)

# Long-term growth rate (LGR) = Dominant Eigen value
LGR2 = eigen(L2)$values[1]

#Stable age distribution (SAD) = RIGHT dominant EV
SAD2 = eigen(L2)$vectors[,1]

#Reproductive value (RV) = left dominant EV
RV2 = eigen(t(L2))$vectors[,1]





